package com.example.JPetStore.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.JPetStore.Common;
import com.example.JPetStore.domain.Address;
import com.example.JPetStore.domain.User;
import com.example.JPetStore.mappers.AddressMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class AddressDao {

    @Autowired
    AddressMapper addressMapper;

    //查询所有地址
    public ArrayList<Address> queryAddresses(User user) {
        List<Address> addressList = addressMapper.selectList((new QueryWrapper<Address>()).eq("user_id", user.getUserId()));
        ArrayList<Address> addressList2 = new ArrayList<>(addressList);
        return addressList2;
    }

    public void addAddress(String addressContent, String userId) {

        Address address = new Address();
        address.setAddressContent(addressContent);
        address.setUserId(Integer.parseInt(userId));
        addressMapper.insert(address);
    }

    public Address queryAddressById(String addressId, String userId) {
        Address address = addressMapper.selectOne((new QueryWrapper<Address>()).eq("user_id", userId).eq("address_id", addressId));
        return address;
    }

    public void removeAddress(String addressId, String userId) {
        addressMapper.delete((new QueryWrapper<Address>()).eq("user_id", userId).eq("address_id", addressId));

    }
}
